package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐业务实现
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新增套餐
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //1.将套餐信息保存至setmeal表中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //2.获取套餐的id
        Long setmealId = setmeal.getId();

        //3.获取套餐菜品对应的纪录，包含菜品的id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();


        //4.将套餐id插入套餐菜品
        setmealDishes.forEach( setmealDish -> setmealDish.setSetmealId(setmealId) );

        //5.将套餐菜品对应的纪录插入setmeal_dish表中
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 批量刪除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //1.遍历ids，对应的status字段不为1，即全部的套餐状态不为起售
        ids.forEach( id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        } );


        ids.forEach( setmealId -> {
            //2.在setmeal表中删除id对应的记录
            setmealMapper.deleteById(setmealId);

            //3.在setmeal_dish表中删除setmeal_id对应的记录
            setmealDishMapper.deleteBySeteamlId(setmealId);
        });

    }


    /**
     * 根据套餐id查询套餐信息、分类id、分类名称、套餐的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryByIdWithDish(Long id) {
        //1.根据套餐id查询套餐信息、分类id、分类名称
        Setmeal setmeal = setmealMapper.queryById(id);


        //2.根据套餐id查询菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(id);

        //3.赋值给setmealVO对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }


    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.根据套餐id更新setmeal表的记录
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //2.根据套餐id删除setmeal_dish表的记录
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySeteamlId(setmealId);

        //3.将更新后的套餐中的菜品插入setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }


    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        if(status == StatusConstant.ENABLE) {
            //起售套餐时，套餐内的全部菜品必须处于启售状态
            List<Dish> dishs = setmealDishMapper.getBySetmealId(id);
            if (dishs != null && dishs.size() > 0) {
                dishs.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);

    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.list(setmeal);
        return setmeals;
    }


    /**
     * 根据套餐id查询套餐包括的菜品名称、份数、描述、图片
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
